package com.hnblc.blcwms.rest.servlet;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mock.web.DelegatingServletInputStream;
import org.springframework.util.StringUtils;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;
import java.util.*;

public class MAPIHttpServletRequestWrapper  extends HttpServletRequestWrapper {


    private static Logger logger = LoggerFactory.getLogger(MAPIHttpServletRequestWrapper.class);
    private byte[] bytes;
    private String body;
    private Map<String, List<String>> map;
    private int readMap=0;
    private String queryString;
    /**
     * @param request
     *            {@link javax.servlet.http.HttpServletRequest} object.
     * @throws IOException
     */
    public MAPIHttpServletRequestWrapper(HttpServletRequest request) throws IOException {
        super(request);
        bytes = IOUtils.toByteArray(request.getInputStream());
        queryString = request.getQueryString();

    }

    @Override
    public ServletInputStream getInputStream(){
        final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        return new DelegatingServletInputStream(byteArrayInputStream);
    }

    @Override
    public BufferedReader getReader(){
        return new BufferedReader(new InputStreamReader(getInputStream()));
    }

    @Override
    public String getParameter(String name) {
        logger.debug("run into getParameter");
        return super.getParameter(name);
    }

    @Override
    public Map<String, String[]> getParameterMap() {
        logger.debug("run into getParameterMap");
        return super.getParameterMap();
    }

    @Override
    public Enumeration<String> getParameterNames() {
        logger.debug("run into getParameterNames");
        return super.getParameterNames();
    }
    //暂时先改写这一个,其他的也可以修改
  /*  @Override
    public String[] getParameterValues(String name) {
        logger.info("run into getParameterValues");
        try {
            Map<String, List<String>> nameVals = doParameter();
            List<String> list = nameVals.get(name);
            if(list!=null&&list.size()>0){
                return list.toArray(new String[]{});
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return new String[]{};
    }*/
    //这里获取所有参数值的map
    public Map<String, List<String>> doParameter() throws UnsupportedEncodingException {
        if(readMap==0){
            //这里把post里的参数和地址栏参数结合到一起,然后解析

            map = new HashMap<>();
            if (bytes.length>0){
                body = new String(bytes, getCharacterEncoding());
            }
            if (!StringUtils.isEmpty(queryString)){
                body += "&"+queryString;
            }
            if (!StringUtils.isEmpty(body)) {
                String[] nameVals = body.split("&");
                for (String nameVal : nameVals) {
                    String name = nameVal.split("=")[0];
                    String val = nameVal.split("=")[1];
                    if (map.containsKey(name)) {
                        List<String> vals = map.get(name);
                        vals.add(val);
                        map.put(name, vals);
                    } else {
                        List<String> vals = new ArrayList<String>();
                        vals.add(val);
                        map.put(name, vals);
                    }
                }
                readMap = 1;
            }
        }
        return map;
    }
}
